package com.xm.xmapp.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.widget.Toast;

import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;

public class Tools {

	public static boolean isdanchu = true;

	public static void showMessage(Object text) {
		if (text == null)
			text = "提示消息为null";
		Toast.makeText(MyAppcation.getMyAppcation(), text.toString(),
				3000).show();
	}

	public static void showNetMessage(Object text) {

		if (isdanchu) {
			if (text == null)
				text = "提示消息为null";
			Toast.makeText(MyAppcation.getMyAppcation(), text.toString(),
					Toast.LENGTH_SHORT).show();
			isdanchu = false;
		}
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				isdanchu = true;
			}
		}, 2000);

	}

	public static int dp2px(float dpValue) {
		final float scale = MyAppcation.getMyAppcation().getResources()
				.getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public static String getVersion() {
		try {
			PackageManager manager = MyAppcation.getMyAppcation()
					.getPackageManager();
			PackageInfo info = manager.getPackageInfo(MyAppcation
					.getMyAppcation().getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "为获取到版本信息";
		}
	}

	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public static int getVersionCode() {
		try {
			PackageManager manager = MyAppcation.getMyAppcation()
					.getPackageManager();
			PackageInfo info = manager.getPackageInfo(MyAppcation
					.getMyAppcation().getPackageName(), 0);
			int version = info.versionCode;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return 1;
		}
	}

	/**
	 * 获取手机型号
	 * 
	 */
	public static String getMODEL() {
		return android.os.Build.MODEL;
	}
	
	/**
	 * 获取手机系统版本号
	 * 
	 */
	public static String getSYSTEM_MODEL() {
		return android.os.Build.VERSION.RELEASE;
	}

	public static void startPinjia() {

		try {
			Uri uri = Uri.parse("market://details?id="
					+ MyAppcation.getMyAppcation().getPackageName());
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			MyAppcation.getMyAppcation().startActivity(intent);
		} catch (ActivityNotFoundException e) {

			Tools.showMessage("跳转应用市场失败!");
		}
	}

	public static String getMetaInt(String name) {
		ApplicationInfo ai = null;
		try {
			ai = MyAppcation
					.getMyAppcation()
					.getPackageManager()
					.getApplicationInfo(
							MyAppcation.getMyAppcation().getPackageName(),
							PackageManager.GET_META_DATA);
		} catch (PackageManager.NameNotFoundException e) {
			e.printStackTrace();
		}
		Bundle bundle = ai.metaData;
		return bundle.getString(name);
	}

	public static void saveServerError(String result) {
		StringBuffer sb = new StringBuffer();
		sb.append(result);
		// 保存文件
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");// 用于格式化日期,作为日志文件名的一部分
		String time = format.format(new Date());
		String fileName = time + "ServerError.log";

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_MONTH, -30);
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			try {
				File dir = new File(CodeConstant.SerVerRootFileL);
				if (!dir.exists())
					dir.mkdir();

				FileOutputStream fos = new FileOutputStream(dir + "/"
						+ fileName);
				fos.write(sb.toString().getBytes());
				// 发送给开发人员
				// sendCrashLog2PM(dir + fileName);
				fos.close();
				return;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
